package com.qianfeng.dbsm.impl;

import com.qianfeng.dbsm.common.Contants;
import com.qianfeng.dbsm.dao.TbOrderDao;
import com.qianfeng.dbsm.dao.TbOrderReturnsDao;
import com.qianfeng.dbsm.entity.TbOrder;
import com.qianfeng.dbsm.entity.TbOrderReturns;
import com.qianfeng.dbsm.service.TbOrderReturnsService;
import com.qianfeng.dbsm.vo.OrderReturnsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * (TbOrderReturns)表服务实现类
 *
 * @author makejava
 * @since 2022-11-15 17:11:32
 */
@Service("tbOrderReturnsService")
public class TbOrderReturnsServiceImpl implements TbOrderReturnsService {
    @Autowired
    private TbOrderReturnsDao tbOrderReturnsDao;

    @Autowired
    private TbOrderDao orderDao;

    /**
     * 创建退货订单
     * @param returnsVo
     * @return
     */
    @Override
    public String createOrderReturns(OrderReturnsVo returnsVo) {

        int line = 0;
        String returnNumber = "";
        try{
            //判断订单号是否存在 不存在不予处理
            TbOrder order = orderDao.queryByOrderNumber(returnsVo.getOrderNumber());
            if(order == null){
                throw new RuntimeException("订单编号不存在！请输入正确的订单编号！！！");
            }
            //判断是否已经退货过了
            TbOrderReturns temp = tbOrderReturnsDao.queryByOrderNumber(returnsVo.getOrderNumber());
            if(temp != null){
                throw new RuntimeException("当前订单编号已经存在退货信息，不可重复操作！！！");
            }
            TbOrderReturns orderReturns = new TbOrderReturns();
            orderReturns.setOrderNumber(returnsVo.getOrderNumber());
            orderReturns.setCreateTime(new Date());
            orderReturns.setExpressNo(returnsVo.getExpressNo());
            orderReturns.setLogisticsFee(returnsVo.getLogisticsFee());
            orderReturns.setLogisticsResult(returnsVo.getLogisticsResult());
            orderReturns.setReceiverName(returnsVo.getReceiverName());
            orderReturns.setLogisticsType(returnsVo.getLogisticsType());
            orderReturns.setReceiverAddress(returnsVo.getReceiverAddress());
            orderReturns.setReceiverMobile(returnsVo.getReceiverMobile());
            orderReturns.setReceiverZip(returnsVo.getReceiverZip());
            //创建退货编号
            returnNumber = createReturnsNumber();
            orderReturns.setReturnsNo(returnNumber);
            line = tbOrderReturnsDao.insert(orderReturns);
        }catch (Exception e){
            throw new RuntimeException("生成退货信息失败："+e.getMessage());
        }
        return returnNumber;
    }
    private String createReturnsNumber(){
        StringBuffer sb = new StringBuffer();
        sb.append("return");
        //获取当前时间
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        //获取当前时间的字符串
        String nowDate =  simpleDateFormat.format(date);
        sb.append(nowDate);
        //流水号，获取最新的订单流水号进行判断
        String latestReturnNumber = tbOrderReturnsDao.findLatestReturnNumber();
        //如果没有订单，那么就从00001开始
        if(latestReturnNumber == null){
            sb.append(Contants.ORDERPRESTR);
        }else{
            //最新的流水号和nowDate是同一天，那么则流水增加
            String latestOrderDate = latestReturnNumber.substring(5,13);//截取时间
            if(nowDate.equals(latestOrderDate)){
                //获取流水
                String no = latestReturnNumber.substring(13);//截取时间
                //转换成int,并且通过format进行补0
                sb.append(String.format("%05d",Integer.parseInt(no)+1));
            }else{
                //如果最新的流水号和nowDate不是同一天，那么流水从00001开始
                sb.append(Contants.ORDERPRESTR);
            }
        }
        return sb.toString();
    }
}
